﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using BusinnessLogic;

namespace scrumbench
{
    public partial class StartupWindow : Form
    {
        public StartupWindow()
        {
            InitializeComponent();
          
        }
        //Gets usersinfo out DB




        int logincounter = 0;

        private void CancelClick(object sender, EventArgs e)
        {
            this.Close();
        }


        //Method that starts the validation process
        private void LoginValidation()
        {

            string password = this.textboxPasswoord.Text;
            string username = this.textboxUsername.Text;

            bool userconfirmed;
            int userid = 0;
            string firstname = "";
            string name = "";
            string role = "";

            BusinnessLogic.LoginCheck BLLoginchecker = new LoginCheck();

            userconfirmed = BLLoginchecker.VerifyLogin(username, password, ref userid, ref name, ref firstname, ref role);

            if (userconfirmed)
            {
                //Start MainMenu with name and userid (constructor overload uses this)
                MainMenuForm nMainmenuform = new MainMenuForm(username,userid,name,firstname, role);
                this.Visible = false;
                nMainmenuform.Show();
                
            }
            else
            {
                logincounter += 1;
                int loginlefts = 3 - logincounter;
                MessageBox.Show("Incorrect username and/or password.\nYou have " + loginlefts.ToString() + " tries left.");
                this.textboxPasswoord.Clear();
                this.textboxUsername.Clear();
                this.textboxUsername.Focus();

                if (logincounter >= 3)
                {
                    this.Close();
                }
            }


        }


        // Checks userinfo against input data
        private void Login_Click(object sender, EventArgs e)
        {

            LoginValidation();
           


          }

        private void StartupWindow_Load(object sender, EventArgs e)
        {
            this.button1.Focus();
        }

        //Allow "Enter" to be used as validation key
        private void textboxPasswoord_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                LoginValidation();
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        
    }
}
